package com.cy.personer.web;

import com.cy.personer.bean.Desk;
import com.cy.personer.bean.Remark;
import com.cy.personer.bean.Work;
import com.cy.personer.mapper.RemarkMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @施晓彬
 */

@Controller
@RequestMapping("/remark")
public class RemarkHandler {
   // private List<Remark> remarks=null;
    @Resource
    private RemarkMapper remarkMapper;
    @Resource
    private Remark remark;
    @RequestMapping(value = "/remarkList.action")
    public String remarkList(HttpServletRequest request, Integer num, Work work, Desk desk){
        if(num==null||num==0){
            num=1;
        }
        PageHelper.startPage(num,5);
        List<Remark> remarks = remarkMapper.remarkList(work,desk);
        List<Desk> desks = remarkMapper.desks();
        request.setAttribute("desks",desks);
        PageInfo<Remark> page = new PageInfo<>(remarks);
        request.setAttribute("page",page);
        return "admin/remark_list";
    }
    /**
     * ajax执行add的Mapper成功后返回boolean的回调函数
     * @author whl
     */
    @RequestMapping(value="/addRemarks.action",method = RequestMethod.POST,produces="application/json;charset=utf-8")
    public @ResponseBody Boolean addRemarks(@RequestParam String remarkField,String remarkName,Integer workId){
        remark.setWorkId(workId);
        remark.setRemarkField(remarkField);
        remark.setRemarkName(remarkName);
        boolean b = remarkMapper.addRemark(remark);
        if(b){
            return true;
        }else {
          return false;
        }
    }

    /**
     * ajax执行修改的Mapper成功后返回boolean的回调函数
     * @author whl
     */
    @RequestMapping(value="/updateRemark1.action",method = RequestMethod.POST,produces="application/json;charset=utf-8")
    public @ResponseBody Boolean updateRemark1( @RequestParam String remarkField1,String remarkName1,Integer remarkId1){
       remark.setRemarkName(remarkName1);
       remark.setRemarkField(remarkField1);
       remark.setRemarkId(remarkId1);
       boolean b = remarkMapper.updateById(remark);
        if(b){
            return true;
        }else {
            return false;
        }
    }

    /**
     * @author sxb
     * 删除小结评语
     */
    @RequestMapping(value=("/delectRemark.action"),method= RequestMethod.POST, produces="application/json;charset=utf-8")
    public @ResponseBody Boolean delectRemark(@RequestParam Integer remarkId){
        boolean b = remarkMapper.delRemark(remarkId);
        if(b){
         return true;
        }else {
            return false;
        }

    }
}
